home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 April: Mac OS SDK / Dev.CD Apr 99 SDK1.toast / Development Kits / ColorSync 2.5.1 SDK / Sample Code / CSDemo 2.5 / ShellSources / appAEvts.c < prev    next >
Encoding:
Text File  |  1998-09-09  |  15.5 KB  |  574 lines  |  [TEXT/CWIE]

  1. // Simple framework for Macintosh sample code
  2. //
  3. // David Hayward and Nick Thompson 
  4. // Developer Technical Support
  5. // AppleLink: DEVSUPPORT
  6. //
  7. // Copyrite 1995, Apple Computer,Inc
  8. //
  9. // This file contains the framework's AppleEvent handlers
  10. // 
  11. // 9/13/94    nick    first cut
  12. // 12/13/94    david    several modifications
  13.  
  14.  
  15. #include <Types.h>
  16. #include <Memory.h>
  17. #include <AppleEvents.h>
  18. #include <Errors.h>
  19.  
  20. #include "aeUtils.h"
  21.  
  22. #include "appGlobals.h"
  23. #include "appAEvts.h"
  24. #include "appPrint.h"
  25. #include "appErrors.h"
  26.  
  27. #include "win.h"
  28. #include "winTables.h"
  29.  
  30. /**\
  31. |**| ==============================================================================
  32. |**| PRIVATE FUNCTION PROTOTYPES
  33. |**| ==============================================================================
  34. \**/
  35. OSErr appHandleOAPP     ( void ) ;
  36. OSErr appHandleODOC        ( FSSpec *spec ) ;
  37. OSErr appHandlePDOC     ( FSSpec *spec, FSSpec *dtp ) ;
  38. OSErr appHandleQUIT     ( void ) ;
  39.  
  40.  
  41. /**\
  42. |**| ==============================================================================
  43. |**| PUBLIC FUNCTIONS
  44. |**| ==============================================================================
  45. \**/
  46.  
  47.  
  48. //-----------------------------------------------------------------------
  49. // called to register our appleevent handlers
  50.  
  51. OSErr RegisterAppleEvents ( void )
  52. {
  53.     OSErr err ;
  54.     
  55.     err = AEInstallEventHandler( kCoreEventClass, kAEOpenApplication,
  56.                                  NewAEEventHandlerProc(HandleOAPP),
  57.                                  0L, false ) ;
  58.     if ( err ) return err ;
  59.                 
  60.     err = AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments,
  61.                                  NewAEEventHandlerProc(HandleODOC),
  62.                                  0L, false ) ;
  63.     if ( err ) return err ;
  64.                 
  65. #if PIGS_SHELL_PRINT
  66.     err = AEInstallEventHandler( kCoreEventClass, kAEPrintDocuments,
  67.                                  NewAEEventHandlerProc(HandlePDOC),
  68.                                  0L, false ) ;
  69.     if ( err ) return err ;
  70. #endif
  71.                 
  72.     err = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication,
  73.                                  NewAEEventHandlerProc(HandleQUIT),
  74.                                  0L, false ) ;
  75.     return err ;
  76. }
  77.  
  78.  
  79. /*------------------------------------------------------------------------------*\
  80.     HandleOAPP
  81.  *------------------------------------------------------------------------------*
  82.         This routine processes the 'OAPP' AppleEvent
  83.         and passes it to the framework.
  84. \*------------------------------------------------------------------------------*/
  85. pascal OSErr HandleOAPP ( AppleEvent *theAppleEvent, AppleEvent *reply, long refCon )
  86. {
  87.     OSErr err ;
  88.  
  89.     err = MyGotRequiredParams(theAppleEvent) ;
  90.  
  91.     if (err == noErr)                
  92.         err = appHandleOAPP() ;
  93.  
  94.     return err ;
  95. }
  96.  
  97.  
  98. /*------------------------------------------------------------------------------*\
  99.     HandleODOC
  100.  *------------------------------------------------------------------------------*
  101.         This routine processes the 'ODOC' AppleEvent
  102.         and passes it to the framework.
  103. \*------------------------------------------------------------------------------*/
  104. pascal OSErr HandleODOC ( AppleEvent *theAppleEvent, AppleEvent *reply, long refCon )
  105. {
  106.     FSSpec         myFSS ;
  107.     AEDescList    docList ;
  108.     OSErr        err, ignoreErr ;
  109.     long        index, itemsInList ;
  110.     Size         actualSize ;
  111.     AEKeyword    keywd ;
  112.     DescType    returnedType ;
  113.     
  114.     err = AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, &docList ) ;
  115.     if (err == noErr)
  116.     {
  117.         // check for missing required parameters
  118.         err = MyGotRequiredParams(theAppleEvent) ;
  119.         if (err == noErr)
  120.         {
  121.             // see how many descriptor items are in the list
  122.             // this is the number of documents we want to open
  123.             err = AECountItems(&docList,&itemsInList) ;
  124.     
  125.             // now get each descriptor record from the list
  126.             // coerce the returned data to an FSSpec record, and
  127.             // open the asoociated file
  128.             
  129.             for (index=1; index <= itemsInList && err == noErr; index++)
  130.             {
  131.                 err = AEGetNthPtr(    &docList, 
  132.                                     index,
  133.                                     typeFSS,
  134.                                     &keywd,
  135.                                     &returnedType,
  136.                                     (Ptr)&myFSS,
  137.                                     sizeof(myFSS),
  138.                                     &actualSize) ;
  139.         
  140.                 if (err == noErr)
  141.                     err = appHandleODOC( &myFSS ) ;
  142.             }
  143.         }
  144.         ignoreErr = AEDisposeDesc(&docList) ;
  145.     }
  146.     return err ;
  147. }
  148.  
  149.  
  150. #if PIGS_SHELL_PRINT
  151. /*------------------------------------------------------------------------------*\
  152.     HandlePDOC
  153.  *------------------------------------------------------------------------------*
  154.         This routine processes the 'PDOC' AppleEvent
  155.         and passes it to the framework.
  156. \*------------------------------------------------------------------------------*/
  157. pascal OSErr HandlePDOC ( AppleEvent *theAppleEvent, AppleEvent *reply, long refCon )
  158. {
  159.     FSSpec         myFSS, dtpFSS ;
  160.     AEDescList    docList, dtpList ;
  161.     OSErr        err ;
  162.     long        index, itemsInList ;
  163.     Size         actualSize ;
  164.     AEKeyword    keywd ;
  165.     DescType    returnedType ;
  166.     Boolean        draggedToDTP = false ;
  167.     
  168.     err = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&docList) ;
  169.     if (err == noErr)
  170.     {
  171.         // Check to see if the user dragged the document to a desktop printer.
  172.         err = AEGetParamDesc(theAppleEvent, keyOptionalKeywordAttr,typeAEList,&dtpList) ;
  173.         if (err == noErr) draggedToDTP = true ;
  174.  
  175.         // check for missing required parameters
  176.         err = MyGotRequiredParams(theAppleEvent) ;
  177.         if (err == noErr)
  178.         {
  179.             // get the desktop printer that the file(s) were draged to
  180.             if (draggedToDTP)
  181.             {
  182.                 err = AEGetNthPtr(&dtpList,
  183.                                     1,
  184.                                     typeFSS,
  185.                                     &keywd, 
  186.                                     &returnedType,
  187.                                     (Ptr) &dtpFSS, 
  188.                                     sizeof(FSSpec),
  189.                                     &actualSize) ;
  190.                 AEDisposeDesc(&dtpList) ;
  191.             }
  192.             
  193.             // see how many descriptor items are in the list
  194.             // this is the number of documents we want to open
  195.             err = AECountItems(&docList,&itemsInList) ;
  196.     
  197.             // now get each descriptor record from the list
  198.             // coerce the returned data to an FSSpec record, and
  199.             // open the asoociated file
  200.             
  201.             for (index=1; index <= itemsInList && err == noErr; index++)
  202.             {
  203.                 err = AEGetNthPtr(    &docList, 
  204.                                     index,
  205.                                     typeFSS,
  206.                                     &keywd,
  207.                                     &returnedType,
  208.                                     (Ptr)&myFSS,
  209.                                     sizeof(myFSS),
  210.                                     &actualSize) ;
  211.                 if (err == noErr)
  212.                 {
  213.                     if (draggedToDTP)            
  214.                         err = appHandlePDOC( &myFSS, &dtpFSS ) ;
  215.                     else
  216.                         err = appHandlePDOC( &myFSS, nil ) ;
  217.                 }
  218.             }
  219.         }
  220.         err = AEDisposeDesc(&docList) ;
  221.     }
  222.     return err ;
  223. }
  224. #endif
  225.  
  226.  
  227. /*------------------------------------------------------------------------------*\
  228.     HandleQUIT
  229.  *------------------------------------------------------------------------------*
  230.         This routine processes the 'QUIT' AppleEvent
  231.         and passes it to the framework.
  232. \*------------------------------------------------------------------------------*/
  233. pascal OSErr HandleQUIT ( AppleEvent *theAppleEvent, AppleEvent *reply, long refCon )
  234. {
  235.     OSErr err ;
  236.  
  237.     err = MyGotRequiredParams(theAppleEvent) ;
  238.  
  239.     if (err == noErr)                
  240.         err = appHandleQUIT() ;
  241.  
  242.     if (err == noErr)    
  243.     {            
  244.           AEDisposeDesc(&gSelfAddress) ;            // Dispose of my self-addressed descriptor.
  245.           AEDisposeDesc(&gSelfPSNAddress) ;        // Dispose of my self-addressed descriptor.
  246.     }
  247.     return err ;
  248. }
  249.  
  250.  
  251. /*------------------------------------------------------------------------------*\
  252.     SendQUIT
  253.  *------------------------------------------------------------------------------*
  254.         This routine sends the 'QUIT' AppleEvent to this application to
  255.         terminate this app.  This routine is called by DoAppQuitCommand.
  256. \*------------------------------------------------------------------------------*/
  257. void SendQUIT ( void )
  258. {
  259.     AppleEvent    myAppleEvent, reply ;
  260.     
  261.     //    Create the Apple Event.
  262.     FailIfErr(AECreateAppleEvent( kCoreEventClass, 
  263.                                   kAEQuitApplication, 
  264.                                   &gSelfAddress,
  265.                                   kAutoGenerateReturnID, 
  266.                                   kAnyTransactionID, 
  267.                                   &myAppleEvent)) ;
  268.                                   
  269.     //    Send the Apple Event.
  270.       FailIfErr(AESend( &myAppleEvent, 
  271.                         &reply, 
  272.                         kAENoReply+kAENeverInteract, 
  273.                         kAENormalPriority,
  274.                         kAEDefaultTimeout, 
  275.                         nil, 
  276.                         nil)) ;
  277.                         
  278.       AEDisposeDesc(&myAppleEvent) ;                // Dispose of the Apple Event.
  279. }
  280.  
  281.  
  282.  
  283. /*------------------------------------------------------------------------------*\
  284.     SendODOC
  285.  *------------------------------------------------------------------------------*
  286.         This routine sends the 'ODOC' AppleEvent to this application to
  287.         open a document.  This routine is called by DoAppOpenCommand.
  288. \*------------------------------------------------------------------------------*/
  289. void SendODOC (FSSpec *myFSSpec)
  290. {
  291.      AppleEvent    myAppleEvent ;
  292.     AppleEvent    defReply ;
  293.     AEDescList    docList ;
  294.     OSErr         ignoreErr ;
  295.     
  296.     myAppleEvent.dataHandle = nil ;
  297.     docList.dataHandle  = nil ;
  298.     defReply.dataHandle = nil ;
  299.         
  300.     // Create empty list and add one file spec
  301.     FailIfErr(AECreateList(nil,0,false, &docList)) ;
  302.     
  303.     FailIfErr(AEPutPtr(&docList,1,typeFSS,(Ptr)myFSSpec,sizeof(FSSpec))) ;
  304.         
  305.     FailIfErr(AECreateAppleEvent(    kCoreEventClass,
  306.                                     kAEOpenDocuments,
  307.                                     &gSelfAddress,
  308.                                     kAutoGenerateReturnID,
  309.                                     kAnyTransactionID,
  310.                                     &myAppleEvent)) ;
  311.  
  312.     // Put Params into our event and send it
  313.  
  314.     FailIfErr(AEPutParamDesc( &myAppleEvent,
  315.                               keyDirectObject,
  316.                               &docList)) ;
  317.  
  318.     FailIfErr(AESend( &myAppleEvent,
  319.                       &defReply,
  320.                       kAENoReply+kAENeverInteract,
  321.                       kAENormalPriority,
  322.                       kAEDefaultTimeout,
  323.                       nil,
  324.                       nil)) ;
  325.         
  326.         
  327.     if (myAppleEvent.dataHandle) 
  328.         ignoreErr = AEDisposeDesc(&myAppleEvent) ;
  329.         
  330.     if (docList.dataHandle) 
  331.         ignoreErr = AEDisposeDesc(&docList) ;
  332. }
  333.  
  334.  
  335.  
  336. #if PIGS_SHELL_PRINT
  337. /*------------------------------------------------------------------------------*\
  338.     SendPDOC
  339.  *------------------------------------------------------------------------------*
  340.         This routine sends the 'PDOC' AppleEvent to this application to
  341.         print a document.  This routine is called by DoAppPrintCommand and
  342.         DoAppPrintOneCommand.
  343. \*------------------------------------------------------------------------------*/
  344. void SendPDOC (FSSpec *myFSSpec)
  345. {
  346.      AppleEvent    myAppleEvent ;
  347.     AppleEvent    defReply ;
  348.     AEDescList    docList ;
  349.     OSErr         ignoreErr ;
  350.     
  351.     myAppleEvent.dataHandle = nil ;
  352.     docList.dataHandle  = nil ;
  353.     defReply.dataHandle = nil ;
  354.         
  355.     // Create empty list and add one file spec
  356.     FailIfErr(AECreateList(nil,0,false, &docList)) ;
  357.     
  358.     FailIfErr(AEPutPtr(&docList,1,typeFSS,(Ptr)myFSSpec,sizeof(FSSpec))) ;
  359.         
  360.     FailIfErr(AECreateAppleEvent(    kCoreEventClass,
  361.                                     kAEPrintDocuments,
  362.                                     &gSelfAddress,
  363.                                     kAutoGenerateReturnID,
  364.                                     kAnyTransactionID,
  365.                                     &myAppleEvent)) ;
  366.  
  367.     // Put Params into our event and send it
  368.  
  369.     FailIfErr(AEPutParamDesc( &myAppleEvent,
  370.                               keyDirectObject,
  371.                               &docList)) ;
  372.  
  373.     FailIfErr(AESend( &myAppleEvent,
  374.                       &defReply,
  375.                       kAENoReply+kAENeverInteract,
  376.                       kAENormalPriority,
  377.                       kAEDefaultTimeout,
  378.                       nil,
  379.                       nil)) ;
  380.         
  381.         
  382.     if (myAppleEvent.dataHandle) 
  383.         ignoreErr = AEDisposeDesc(&myAppleEvent) ;
  384.         
  385.     if (docList.dataHandle) 
  386.         ignoreErr = AEDisposeDesc(&docList) ;
  387. }
  388. #endif
  389.  
  390.  
  391. /**\
  392. |**| ==============================================================================
  393. |**| PRIVATE FUNCTIONS
  394. |**| ==============================================================================
  395. \**/
  396.  
  397.  
  398. /*------------------------------------------------------------------------------*\
  399.     appHandleOAPP
  400.  *------------------------------------------------------------------------------*
  401.         This routine handles the 'OAPP' AppleEvent for the framework.
  402.         This routine is called by HandleOAPP.
  403. \*------------------------------------------------------------------------------*/
  404. static OSErr appHandleOAPP ( void ) 
  405. {
  406.     OSErr            err = noErr ;
  407.     winHandle        win;
  408.     AllocProcPtr    allocProc = nil ;
  409.     short            i ;
  410.     
  411.     // determine what type of winHandles we need to auto-new
  412.     for ( i=0; !err && i<gAllocProcMapCount; i++ )
  413.     {
  414.         if (gAllocProcMap[i].autoNew)
  415.         {
  416.             allocProc = gAllocProcMap[i].AllocProc ;
  417.  
  418.             // create a new winHandle of the propper type
  419.             err = NewWinHandle( &win, allocProc ) ;
  420.             WarnIfErr( err ) ;
  421.             if (err) return err;
  422.         
  423.             err = CallWinNewProc( win ) ;
  424.             if ( err != noErr )
  425.                 DisposeWinHandle( win ) ;
  426.         
  427.             if ( err == kWasAlreadyOpen )
  428.                 err = noErr ;
  429.         }
  430.     }
  431.  
  432.     return err ;
  433. }
  434.  
  435.  
  436. #if PIGS_SHELL_PRINT
  437. /*------------------------------------------------------------------------------*\
  438.     appHandlePDOC
  439.  *------------------------------------------------------------------------------*
  440.         This routine handles the 'PDOC' AppleEvent for the framework.
  441.         This routine is called by HandlePDOC.
  442. \*------------------------------------------------------------------------------*/
  443. static OSErr appHandlePDOC ( FSSpec *spec, FSSpec *dtp ) 
  444. {
  445.     OSErr            err = noErr ;
  446.     winHandle        win ;
  447.     FInfo            info ;
  448.     AllocProcPtr    allocProc = nil ;
  449.     Boolean            wasAlreadyOpen = false ;
  450.     short            i ;
  451.     
  452.     // get the file info for the FSSpec
  453.     err = FSpGetFInfo(spec, &info) ;
  454.     if ( err != noErr ) return err ;
  455.     
  456.     // determine what type of winHandle we need
  457.     for ( i=0; i<gAllocProcMapCount; i++ )
  458.     {
  459.         if (  (gAllocProcMap[i].filetype == info.fdType)
  460.            && (gAllocProcMap[i].printable == true ) )
  461.             allocProc = gAllocProcMap[i].AllocProc ;
  462.     }
  463.     if ( allocProc==nil )
  464.         return WarnIfErr( eWarnCantOpenFile ) ;
  465.  
  466.     // create a new winHandle of the proper type
  467.     err = NewWinHandle( &win, allocProc ) ;
  468.     WarnIfErr( err ) ;
  469.     if (err) return err ;
  470.  
  471.     // set this field of the winHandle so that the doc can be opened
  472.     SetWinFSSpec ( win, spec ) ;
  473.     
  474.     err = CallWinOpenProc ( win ) ;
  475.     if ( err != noErr )                        //if an error occured
  476.         DisposeWinHandle( win ) ;
  477.  
  478.     if ( err == kWasAlreadyOpen )
  479.     {
  480.         wasAlreadyOpen = true ;
  481.         err = noErr ;
  482.     }
  483.     
  484.     if ( err ) return err ;
  485.         
  486.     // we know the document window is frontmost because we opened it
  487.     win = GetWindowWinHandle( FrontWindow() ) ;
  488.     if ( win == nil ) return nilHandleErr ;
  489.     
  490. //    if ( dtp && GXPrinting_present()==noErr )
  491. //        GXSelectJobOutputPrinter( GetWinGXJob(win) , dtp->name) ;
  492.     DoAppPrintLoop( win ) ;                    // print w/o dialogs
  493.     
  494.     if( !wasAlreadyOpen )                    // do we need to close it?
  495.         CallWinCloseProc( win ) ;            // close the window
  496.     
  497.     return err ;
  498. }
  499. #endif
  500.  
  501.  
  502. /*------------------------------------------------------------------------------*\
  503.     appHandleODOC
  504.  *------------------------------------------------------------------------------*
  505.         This routine handles the 'ODOC' AppleEvent for the framework.
  506.         This routine is called by HandleODOC.
  507. \*------------------------------------------------------------------------------*/
  508. static OSErr appHandleODOC ( FSSpec *spec ) 
  509. {
  510.     OSErr            err = noErr ;
  511.     winHandle        win ;
  512.     FInfo            info ;
  513.     AllocProcPtr    allocProc = nil ;
  514.     OSType            subtype = nil;
  515.     short            i ;
  516.     
  517.     // get the file info for the FSSpec
  518.     err = FSpGetFInfo(spec, &info) ;
  519.     if ( err != noErr ) return err ;
  520.     
  521.     // determine what type of winHandle we need
  522.     for ( i=0; i<gAllocProcMapCount; i++ )
  523.     {
  524.         if (gAllocProcMap[i].filetype == info.fdType)
  525.         {
  526.             allocProc = gAllocProcMap[i].AllocProc ;
  527.             subtype = gAllocProcMap[i].subtype ;
  528.         }
  529.     }
  530.     if ( allocProc==nil )
  531.         return WarnIfErr( eWarnCantOpenFile ) ;
  532.  
  533.     // create a new winHandle of the proper type
  534.     err = NewWinHandle( &win, allocProc ) ;
  535.     WarnIfErr( err ) ;
  536.     if (err) return err ;
  537.  
  538.     // set these field of the winHandle so that the doc can be opened
  539.     SetWinFSSpec ( win, spec ) ;
  540.     SetWinSubtype ( win, subtype ) ;
  541.     
  542.     err = CallWinOpenProc ( win ) ;
  543.     if ( err != noErr )                        //if an error occured
  544.         DisposeWinHandle( win ) ;
  545.     
  546.     if ( err == kWasAlreadyOpen )
  547.         err = noErr ;
  548.     
  549.     return err ;
  550. }
  551.  
  552.  
  553. /*------------------------------------------------------------------------------*\
  554.     appHandleQUIT
  555.  *------------------------------------------------------------------------------*
  556.         This routine handles the 'QUIT' AppleEvent for the framework.
  557.         This routine is called by HandleQUIT.
  558. \*------------------------------------------------------------------------------*/
  559. static OSErr appHandleQUIT ( void ) 
  560. {
  561.     winHandle        win;
  562.     
  563.     // close all windows
  564.     do {
  565.         win = FrontWin();
  566.         CallWinCloseProc(win);
  567.     }
  568.     while (win);
  569.     
  570.     // signal to Quit
  571.     gQuitFlag = true ;
  572.     return noErr ;
  573. }
  574.